package com.lanyou.esb.redis.proxy.impl;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.lanyou.esb.common.Constants4Redis;
import com.lanyou.esb.redis.JedisPoolUtils;
import com.lanyou.esb.redis.proxy.base.BaseStrJedisProxy;

/**
 * 并发数Redis操作代理
 * 
 * @author Davey.wu
 */
@Service
public class ConcurrencyNumberJedisProxy extends BaseStrJedisProxy<Double> {
	/** 精确到秒 **/
	private static final SimpleDateFormat DATEFORMATE = new SimpleDateFormat(
			Constants4Redis.DATE_PATTERN_YYYYMMDDHHMMSS_NOSPACE);

	public ConcurrencyNumberJedisProxy() {
		this.setTimeOut(Constants4Redis.REDIS_TIMEOUT_CONCURRENCY_NUMBER);
		this.setPrefix(Constants4Redis.PREFIX_CONCURRENCY_NUMBER);
	}

	@Override
	public long setex(String key, Double target) throws JsonProcessingException {
		Double result = 0.0;
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return 0l;
		}
		boolean broken = false;
		try {
			result = jedis.incrByFloat(key, target);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		if (result > 0) {
			return 1l;
		}
		return 0;
	}

	@Override
	public Double get(String key) throws JsonParseException,
			JsonMappingException, IOException {
		String value = "";

		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return 0.0;
		}
		boolean broken = false;
		try {
			value = jedis.get(key);
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		if (StringUtils.isBlank(value)) {
			return 0.0;
		}
		try {
			return Double.parseDouble(value);
		} catch (Exception e) {
			return 0.0;
		}
	}

	public Map<String, Double> get(List<String> keys)
			throws JsonParseException, JsonMappingException, IOException {
		if (keys == null || keys.isEmpty()) {
			return null;
		}
		Jedis jedis = JedisPoolUtils.getJedis();
		if (jedis == null) {
			return null;
		}
		boolean broken = false;
		Map<String, Response<String>> map = new HashMap<>(keys.size());
		try {
			Pipeline pipe = jedis.pipelined();
			for (String key : keys) {
				map.put(key, pipe.get(key));
			}
			pipe.sync();
		} catch (Exception e) {
			broken = true;
		} finally {
			JedisPoolUtils.returnRes(jedis, broken);
		}
		if (map.isEmpty()) {
			return null;
		}
		Map<String, Double> result = new HashMap<>(map.size());
		for (Entry<String, Response<String>> entry : map.entrySet()) {
			String value = entry.getValue().get();
			try {
				result.put(entry.getKey(), Double.parseDouble(value));
			} catch (Exception e) {
				// 没有值，默认为0
				result.put(entry.getKey(), 0.0);
			}
		}
		return result;
	}

	@Override
	public String generateKey(String field) {
		return this.getPrefix() + KEY_SEPARATOR + field;
	}

	public String generateKey(Date date) {
		return this.getPrefix() + KEY_SEPARATOR + DATEFORMATE.format(date);
	}

	@Override
	public String generateKeyFromObject(Double target) {
		// TODO Auto-generated method stub
		return null;
	}
}
